Now let's save off our equipment to our data store.
I am in the game manager.
The game manager is and server script service game manager.
And then we'll go near the top of the script where we're doing our declaration of variables, local
equip, and then I'll just do equip utils.
We're going to require our module script game server, script service and then equip storage utils.
Cool.
Now we can call stuff from our equipment utils.
Let's go down to our initialized player data.
And in here.
If we get data they played.
So we need to initialize the player who's entering the game.
Let's go down below our streak information and I'm going to do an if statement here.
I'll do if.
Data equipment data dot equipment then equip utils colon update player with data info.
There we go.
I'm going to do a player comma whoops data dot equipment.
So if we have equipment then we're going to go over to the module script and update our equipment table.
With that equipment.
Let's go to our equipment storage utils and look for that, that method and there's nothing in it.
So we're going to have to do that now.
Turns out this is going to be incredibly easy.
We're getting our data, which is our data equipment, and then we'll get our equipment table for the
player equals data, and we're done with that.
That's pretty cool.
This one's going to be a little trickier.
Let's work on this now because we're going to implement that in a game manager when we die or when we're
coming into the world, even if it's the first time.
So this one is going to be the hardest of them, but it's still not that bad.
We're going to say if equipment table.
For the player, then let's get the character from the player.
Character equals I'll do char equals character or player dot character added.
Wait, just in case the character is being loaded.
I get that spelled right.
All right, so we get the character either way.
Cool.
And then I'm going to do an if statement.
No, let's do a for let's do four I and V in pairs.
There we go.
We'll get our equipment table square bracket player.
We're going to get all the elements in there and do we'll do a variable local.
I'm going to call this backpack item.
We have to check to see if they have the item already.
This is going to have a race condition where when we're entering the world, it might get called more
than one time.
So we have to make the function item potent, which means you can call it as many times as you want,
as long as you call it once, it's not going to make a difference if you call it two times or ten times.
So we're going to get our player backpack colon, find first child V dot name.
All right.
And then we'll do local item.
And that's going to be a quip.
Diatom char colon Find first child.
V dot name.
So we're going to check to see if it's in the backpack.
We're going to check to see if it's equipped.
Right now what we have to do is say, if not be P item, not backpack item and not E item not equipped
item.
Then local item equals replicated storage.
Wait for child V name clone.
And then we would also.
Oh, don't we have replicated storage here?
We don't.
That's all right.
Let's go ahead and get a variable for us in case we need it elsewhere.
We'll put it at the top.
Scroll on up here, maybe underneath our equipment table.
Local R's game, get service, replicated storage.
Now we can get our items.
Let's go back.
Ah, here we go.
So we're going to clone the item.
This is also where we would check for configuration stuff.
We don't have any of that implemented, but when we start adding it, we'll start doing it.
We have the cooldown, but the cooldown is always the same on the pistol, so we're not going to have
a problem with that.
Once we clone our item, we'll put the parent as the player bat backpack, so we're going to stick the
item in the backpack when they enter the game, either if they die or for the first time.
All right, we got one more.
We may as well do it right.
We have this remove player.
This is actually really easy to.
We're going to get our equipment table for the player and then we're just going to equal nil.
And that's going to remove all the data for that player when they leave the game so that our table doesn't
grow ad infinitum.
Well, or until the server is turned off and reset.
Nice.
I'm going to get rid of that line there.
Let's add one more simple function to our equip storage utils.
I'm going to.
I'm going to add it below equip player and above remove player, but it doesn't really matter where
you add it.
Let's do a function equip utils and I'm going to call this get player items to save and we'll pass in
the player.
And then also I'm want to do is return the equipment table for that player.
That's going to make things a little simpler on our game manager.
That way we don't have to do data storage inside this Utils script.
Let's go back to our game manager now.
Let's go down to save player data.
All right?
So if you do save player data.
We need to save off of our save off our equipment.
I'm going to do it under the Daily Streak stuff.
And oh, man, I made it lowercase.
That's all right.
So we got our we have our equipment, which I messed up the case.
Just keep it consistent.
You can change all that if you want.
Make it an uppercase.
But I'm already this far in.
I'm going to say data dot equipment.
Then I'm going to get my what would you call it?
Equipment utils.
And I'm going to get player items to save.
And there's my player, right?
So that is going to put all of my equipment inside that data and then we're going to do our set async
wallah.
It is saved.
When we're doing a player removing let's go down to.
Player removing.
Look, the only thing we do when we do player removing is save the data.
We're gonna have to change this a little bit.
We're going to have to do a function.
The player will get passed in and then we kind of messed that up.
Got to put an end here.
Save player data, that's fine.
But we now have to pass in the player because we didn't just connect the function, we connected the
function to an anonymous function.
Now I want to get my equipment utils.
And remove the player from the equipment table.
There we go.
All right, one more thing we got to do.
I know.
I probably keep saying that.
Let's go to our player added event.
All right, so player is added, we add the board, we initialize the player data, meaning we get any
equipment data that's already there.
So what I can do here is equip utils, equip player.
And then pass in the player.
Now, here's why we needed something that was and potent for our equipped player.
We need to be able to call it more than once when we connect our character added because initialized
player data could take a little bit of time.
This might get added first, but we have to make sure that we add this.
Control c to r character added event because when we die we want to get our equipment.
Where should I add that?
I could add that at the top.
So we have our character added.
And then we'll just paste that again.
So if you don't have this in both spots, sometimes when you enter the world, you're not going to get
equipment.
The player, the character will get added and you're going to get a race condition.
So you're going to need it for both places.
So you have that's why you have to do the checks to make sure that the equipment is there.
I think we're good to go.
Let's play our game and see if it functions correctly.
Now we're not going to have anything starting out because we weren't saving anything.
Let's take a look at our output window.
The output window looks good.
We don't have any errors.
I'll just make this a little smaller and then if something interesting pops up, I'll make it bigger.
We're going to buy simple pistol.
There we go.
We're going to buy Fabulous Lemon.
There we go.
We are going to die.
Yeah.
Reset.
There we go.
We got it.
Now, if we leave the game.
Started back up.
I look at that and no errors.
So we have some data storage.
That's pretty cool.
All right.
I'll see you in the next video.